﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaveMaster.Holidays.Club.Gateway;
using WaveMaster.Holidays.Club.Domain;
using System.Collections;
using MySql.Data.MySqlClient;
using WaveMaster.Holidays.Club.SQL;

namespace WaveMaster.Holidays.Club.Service
{
    public class SeaPortService : IService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public SeaPortService(DataBaseConnectivity dataBaseConectivity)
        {
            m_dataBaseConectivity = dataBaseConectivity;
        }
        public SeaPortService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public IList LoadAllSeaPort()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.GetAllSeaPort(), m_dataBaseConectivity.InterfaceSqlConnection);
                //LogService.WriteLogExecuteSQLCommand(cmd);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    SeaPort user = new SeaPort(rd);
                    result.Add(user);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public IList LoadAllActiveSeaPort()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.GetAllActiveSeaPort(), m_dataBaseConectivity.InterfaceSqlConnection);
                //LogService.WriteLogExecuteSQLCommand(cmd);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    SeaPort user = new SeaPort(rd);
                    result.Add(user);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }
        public SeaPort GetSeaPortByID(long id)
        {
            SeaPort result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.GetSeaPortByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_seaPortID", MySqlDbType.Int32).Value = id;
                //LogService.WriteLogExecuteSQLCommand(cmd);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result = new SeaPort(rd);
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public IList SearchSeaPortByCodeName(string p)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.SearchSeaPortByCodeName(p), m_dataBaseConectivity.InterfaceSqlConnection);
                //cmd.Parameters.Add("code", MySqlDbType.VarChar).Value = p;
                //LogService.WriteLogExecuteSQLCommand(cmd);
                MySqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new SeaPort(rd));
                }
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
            return result;
        }

        public void DeleteSeaPort(SeaPort user)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.DeleteSeaPortByID(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_seaPortID", MySqlDbType.Int32).Value = user.m_seaPortID;
                //LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public void UpdateSeaPort(SeaPort m_user)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.UpdateSeaPort(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_seaPortCode", MySqlDbType.VarChar).Value = m_user.m_seaPortCode;
                cmd.Parameters.Add("m_seaPortName", MySqlDbType.VarChar).Value = m_user.m_seaPortName;
                cmd.Parameters.Add("m_country", MySqlDbType.VarChar).Value = m_user.m_country;
                cmd.Parameters.Add("m_remark", MySqlDbType.VarChar).Value = m_user.m_remark;
                cmd.Parameters.Add("m_modfctnDate", MySqlDbType.Datetime).Value = DateTime.Now; //m_user.UserName;
                cmd.Parameters.Add("m_modfctnBy", MySqlDbType.Int32).Value = m_user.m_modfctnBy;
                cmd.Parameters.Add("m_active", MySqlDbType.VarChar).Value = m_user.m_active;
                cmd.Parameters.Add("m_seaPortID", MySqlDbType.Int32).Value = m_user.m_seaPortID;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            } 
        }

        public void InserSeaPort(SeaPort m_user)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                MySqlCommand cmd = new MySqlCommand(SeaPortSQL.InsertSeaPort(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("m_seaPortCode", MySqlDbType.VarChar).Value = m_user.m_seaPortCode;
                cmd.Parameters.Add("m_seaPortName", MySqlDbType.VarChar).Value = m_user.m_seaPortName;
                cmd.Parameters.Add("m_country", MySqlDbType.VarChar).Value = m_user.m_country;
                cmd.Parameters.Add("m_remark", MySqlDbType.VarChar).Value = m_user.m_remark;
                cmd.Parameters.Add("m_cretnDate", MySqlDbType.Datetime).Value = DateTime.Now;//m_user.CreatenDate;
                cmd.Parameters.Add("m_cretnBy", MySqlDbType.Int32).Value = m_user.m_cretnBy;
                cmd.Parameters.Add("m_modfctnDate", MySqlDbType.Datetime).Value = DateTime.Now; //m_user.UserName;
                cmd.Parameters.Add("m_modfctnBy", MySqlDbType.Int32).Value = m_user.m_cretnBy;
                cmd.Parameters.Add("m_active", MySqlDbType.VarChar).Value = m_user.m_active;

                //LogService.WriteLogExecuteSQLCommand(cmd);
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
    }
}
